package jmvm.schemes;

import java.awt.Point;
import java.util.ArrayList;

import jmvm.components.*;
import jmvm.gui.GuiLabel;
import jmvm.gui.GuiLine;
import jmvm.gui.GuiScheme;


public class MemUprav {
	
	public static OR rdMEM_or_wrMEM;
	
	public static NOT not_fcMEM;
	
	public static AND incMEMACC;
	
	public static REG MEMACC;
	
	public static CMP cmp;
	
	public static REG TIME; // konstantan
	
	public static AND MEMout;
	
	////////////
	//////////// Grafika
	////////////
	
	public static GuiScheme gui;

	public static GuiScheme getGui() {
		return gui;
	}
	
	public static void gui() {

		GuiLine line; // Pomocna promenljiva
		gui = new GuiScheme("jmvm/jmvm/images/MemUprav.png");
		
		ArrayList<ArrayList<Point>> sections;
		ArrayList<Point> points;
		
		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(110,96));
		points.add(new Point(274,96));
		sections.add(points);
		line=new GuiLine(sections,cmp.getEQL());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(145,95));
		points.add(new Point(145,81));
		points.add(new Point(151,81));
		sections.add(points);
		line=new GuiLine(sections,cmp.getEQL());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(453,191));
		points.add(new Point(472,191));
		sections.add(points);
		line=new GuiLine(sections,cmp.getEQL());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(110,62));
		points.add(new Point(118,62));
		sections.add(points);
		line=new GuiLine(sections,MemOper.rdMEM.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(110,75));
		points.add(new Point(119,75));
		sections.add(points);
		line=new GuiLine(sections,MemOper.wrMEM.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(434,75));
		points.add(new Point(440,75));
		sections.add(points);
		line=new GuiLine(sections,new Pin(false));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(181,75));
		points.add(new Point(274,75));
		sections.add(points);
		line=new GuiLine(sections,incMEMACC.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(142,69));
		points.add(new Point(156,69));
		sections.add(points);
		line=new GuiLine(sections,rdMEM_or_wrMEM.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(354,108));
		points.add(new Point(354,170));
		points.add(new Point(373,170));
		sections.add(points);
		line=new GuiLine(sections,MEMACC.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(354,275));
		points.add(new Point(354,212));
		points.add(new Point(373,212));
		sections.add(points);
		line=new GuiLine(sections,TIME.getOut(0));
		gui.addLine(line);

		//
		//
		
		gui.addLabel(new GuiLabel(360, 142, MEMACC.getOut(0)));
		gui.addLabel(new GuiLabel(363, 255,  TIME.getOut(0)));

	}
	/////////////
	/////////////
	/////////////
	
	public static void init() {
		
		rdMEM_or_wrMEM = new OR();
		
		not_fcMEM = new NOT();
		
		incMEMACC = new AND();
		
		MEMACC = new REG(8, "MEMACC");
		MEMACC.getOut(0).setIsInt();
		MEMACC.getOut(0).setNumOfLines(8);
		//MEMACC.setClock(3);
		
		cmp = new CMP(1);
		
		TIME = new REG(8, "TIME");
		TIME.getOut(0).setIsInt();
		TIME.getOut(0).setNumOfLines(8);
		//TIME.setClock(3);
		TIME.initVal(2);
		
		MEMout = new AND();
		
		
	}
	
	public static void connect() {
		
		rdMEM_or_wrMEM.setInputPin(0, MemOper.rdMEM.getOut(0));
		rdMEM_or_wrMEM.setInputPin(1, MemOper.wrMEM.getOut(0));
		
		not_fcMEM.setInputPin(0, cmp.getEQL());
		
		incMEMACC.setInputPin(0, rdMEM_or_wrMEM.getOut(0));
		incMEMACC.setInputPin(1, not_fcMEM.getOut(0));
		
		MEMACC.setInputPin(0, new Pin(0,8));
		MEMACC.setPinLd(new Pin(false));
		MEMACC.setPinInc(incMEMACC.getOut(0));
		MEMACC.setPinCL(cmp.getEQL());
		MEMACC.initVal(0);
		
		cmp.setPinA(0, MEMACC.getOut(0));
		cmp.setPinB(0, TIME.getOut(0));
		
		TIME.setInputPin(0, new Pin(0,8));
		
		MEMout.setInputPin(0, cmp.getEQL());
		MEMout.setInputPin(1, MemOper.rdMEM.getOut(0));
		
	}

	public static void ispisiSignale() {
		
		System.out.println("\n MemUprav:" + " MEMACC="+MEMACC.getOut(0).getIntVal()+" TIME="+TIME.getOut(0).getIntVal());
		
	}

	public static Pin MEMout() {
		return MEMout.getOut(0);
	}


	public static Pin fcMEM() {
		return cmp.getEQL();
	}
}
